package edu.washington.vaccine.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import edu.washington.vaccine.R;
import edu.washington.vaccine.backend.Vaccine;
import edu.washington.vaccine.backend.VaccineLocalHelper;

/*
 * Provides the list of vaccines stored on
 * the local database. The data is determined by the
 * raw/vaccines.txt resource
 */
public class VaccineListActivity extends ListActivity {
	@SuppressWarnings("unused")
	private static final String TAG = "VaccineListActivity";
	android.database.sqlite.SQLiteDatabase db;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        final List<Vaccine> pArray = new ArrayList<Vaccine>();
        
        db = VaccineLocalHelper.getInstance(this).getReadableDatabase();
        
    	Cursor cursor = db.rawQuery("SELECT * FROM Vaccines order by vname", new String [] {});
        startManagingCursor(cursor);
        // output
        while (cursor.moveToNext()) {
        	int vid = cursor.getInt(0);
        	String vname = cursor.getString(1);
//        	int followup = cursor.getInt(2);
//        	int dunv = cursor.getInt(3);
//        	int vdose = cursor.getInt(2);
        	String vdesc = cursor.getString(2);
        	String vschedule = cursor.getString(3);
        	pArray.add(new Vaccine(vid, vname, vdesc, vschedule));
        }
        cursor.close();
    

//		Log.d("array", ""+ pArray.size());
		setListAdapter(new ArrayAdapter<Vaccine>(this, R.layout.list_item, pArray));
	
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Vaccine v = pArray.get(position);
				Intent i = new Intent(VaccineListActivity.this, VaccineDataActivity.class);
				i.putExtra("vaccine", v); 
				VaccineGroupActivity parentActivity = (VaccineGroupActivity)getParent();
				parentActivity.replaceContentView("vaccineInfo", i);
			}
		});
    }
}
